package com.ruoyi.coffee.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.coffee.domain.Products;
import com.ruoyi.coffee.dto.QueryCouponDto;
import com.ruoyi.coffee.vo.CouponVo;
import com.ruoyi.coffee.vo.PageResultVo;
import com.ruoyi.coffee.vo.ProductsVo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Service;
import com.ruoyi.coffee.mapper.CouponMapper;
import com.ruoyi.coffee.domain.Coupon;
import com.ruoyi.coffee.service.ICouponService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 优惠券信息Service业务层处理
 *
 * @author dxj
 * @date 2025-01-08
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements ICouponService {
    @Resource
    private CouponMapper couponMapper;

    /**
     * 查询优惠券信息
     *
     * @param couponId 优惠券信息主键
     * @return 优惠券信息
     */
    @Override
    public Coupon selectCouponByCouponId(Long couponId) {
        return couponMapper.selectCouponByCouponId(couponId);
    }

    /**
     * 查询优惠券信息列表
     *
     * @param coupon 优惠券信息
     * @return 优惠券信息
     */
    @Override
    public List<Coupon> selectCouponList(Coupon coupon) {
        return couponMapper.selectCouponList(coupon);
    }

    /**
     * 新增优惠券信息
     *
     * @param coupon 优惠券信息
     * @return 结果
     */
    @Override
    public int insertCoupon(Coupon coupon) {
        coupon.setCreateTime(DateUtils.getNowDate());
        return couponMapper.insertCoupon(coupon);
    }

    /**
     * 修改优惠券信息
     *
     * @param coupon 优惠券信息
     * @return 结果
     */
    @Override
    public int updateCoupon(Coupon coupon) {
        coupon.setUpdateTime(DateUtils.getNowDate());
        return couponMapper.updateCoupon(coupon);
    }

    /**
     * 批量删除优惠券信息
     *
     * @param couponIds 需要删除的优惠券信息主键
     * @return 结果
     */
    @Override
    public int deleteCouponByCouponIds(Long[] couponIds) {
        return couponMapper.deleteCouponByCouponIds(couponIds);
    }

    /**
     * 删除优惠券信息信息
     *
     * @param couponId 优惠券信息主键
     * @return 结果
     */
    @Override
    public int deleteCouponByCouponId(Long couponId) {
        return couponMapper.deleteCouponByCouponId(couponId);
    }

    /**
     * 分页查询优惠券
     *
     * @param queryCouponDto
     * @return
     */
    @Override
    public AjaxResult getCouponList(QueryCouponDto queryCouponDto) {
        //分页对象
        IPage<Coupon> page = new Page<>(queryCouponDto.getPageNum(), queryCouponDto.getPageSize());
        //查询条件
        LambdaQueryWrapper<Coupon> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.select(Coupon::getCouponId, Coupon::getCouponName, Coupon::getDiscountType,
                Coupon::getDiscountValue, Coupon::getMinimumOrderAmount, Coupon::getTotalQuantity,
                Coupon::getRemainingQuantity, Coupon::getValidForAllProducts, Coupon::getSpecificProductIds,
                Coupon::getExpirationDate);
        lambdaQuery.likeRight(StringUtils.hasText(queryCouponDto.getCouponName()), Coupon::getCouponName, queryCouponDto.getCouponName());
        // 调用 selectPage 分页方法
        IPage<Coupon> couponIPage = baseMapper.selectPage(page, lambdaQuery);
        long total = couponIPage.getTotal();
        List<CouponVo> couponVoList = couponIPage.getRecords().stream()
                .map(coupon -> {
                    CouponVo couponVo = new CouponVo();
                    BeanUtils.copyProperties(coupon, couponVo);
                    return couponVo;
                })
                .collect(Collectors.toList());
        //couponVoList 根据 传递的 isValid进行过滤,1为有效，2为过期
        int isValid = queryCouponDto.getIsValid();
        if (isValid == 1) {
            couponVoList = couponVoList.stream()
                    .filter(couponVo -> couponVo.getExpirationDate().getTime() > System.currentTimeMillis())
                    .collect(Collectors.toList());
        } else {
            couponVoList = couponVoList.stream()
                    .filter(couponVo -> couponVo.getExpirationDate().getTime() < System.currentTimeMillis())
                    .collect(Collectors.toList());
        }
        PageResultVo<CouponVo> resultVo = new PageResultVo<>();
        resultVo.setTotal(total);
        resultVo.setRecords(couponVoList);
        return AjaxResult.success(resultVo);
    }
}
